#this is not necessary but for those who do not like to use new/delete
#DEFS=-DUSE_MALLOC 
INCLUDE=-I. 
## set the compilers
#CXX=/home/gbauer/mpi/mpich2-xl-pwr5-q64/bin/mpicxx
CXX=mpCC
CC=xlc_r
FC=xlf_r

#include compiler flags determined by the CXX
include flags.make

## where mpi/communictor.h 
COMMDIR=../src/
INCLUDE=-I. -I$(COMMDIR)

#DEFS+= -DHAVE_OPENMP 
DEFS+= -DHAVE_MPI -DHAVE_OPENMP -DMPICH_SKIP_MPICXX
#this is to test how the thread-local allocators work
#DEFS+=-DUSE_MALLOAC 

#######################################################################
# comment out for complex-to-complex
# both r2c and c2c are used in dns 
#######################################################################
#FFT_TYPE=-DFFT_R2C

#######################################################################
# use fftw
#######################################################################
#DEFS += -DMYFFT=0 
#FFTW_HOME=/home/jnkim/ppc/fftw_p5
#INCLUDE += -I$(FFTW_HOME)/include -I../src
#FFTW_LIBS = -L$(FFTW_HOME)/lib -lfftw3
#LDFLAGS += -B/usr/share/libhugetlbfs/ -tl -Wl,--hugetlbfs-link=BDT
#######################################################################

#######################################################################
# use essl
#######################################################################
INCLUDE +=  -I../src
DEFS += -DMYFFT=1 
FFTW_LIBS = -L/opt/ibmcmp/xlsmp/1.8/lib64 -lessl -L/opt/ibmcmp/xlf/12.1/lib64 -lxlf90_r -lxl -lxlopt
#FFTW_LIBS = -L/home/gbauer/lib64ppc -lessl -L/opt/ibmcmp/xlf/12.1/lib64 -lxlf90_r -lxl -lxlopt
#LDFLAGS += -B/usr/share/libhugetlbfs/ -tl -Wl,--hugetlbfs-link=BDT

#######################################################################
# use mkl
#######################################################################
#DEFS += -DMYFFT=2
#MKL_HOME=/usr/local/intel/mkl/10.1.2.024
#INCLUDE += -I$(MKL_HOME)/include -I../src
#FFTW_LIBS = -rdynamic -L$(MKL_HOME)/lib/em64t -lmkl -lmkl_lapack -Wl,-rpath,$(MKL_HOME)/lib/em64t
########################################################################

%.o : %.f 
	$(FC) $(FFLAGS) $(CPPFLAGS) -c $< -o $@
%.o : %.cpp 
	$(CXX) $(DEFS) $(CXXFLAGS) $(INCLUDE) -c $< -o $@

.PHONY: all
all: libbase.a rk4_c rk4_f rk4_c2 vorticity_3xyz_c vorticity_3xyz_f vorticity_xy3z_c vorticity_xy3z_f

.PHONY: fft
fft: fft1d_tl fft1d_tl_r

### common objects 
fobjs = dns_fops.o
cobjs = communicator.o
cfiles = mambo.hpp

libbase.a: $(fobjs) $(cobjs)
	ar cru libbase.a $(fobjs) $(cobjs)

rk4_c : $(cfiles) dns_ops.h rk4_tl.cpp libbase.a
	$(CXX) -DRK4_CLOOP $(DEFS) $(CXXFLAGS) $(INCLUDE) $(LDFLAGS) -S -o rk4_c.s rk4_tl.cpp libbase.a
	$(CXX) -DRK4_CLOOP $(DEFS) $(CXXFLAGS) $(INCLUDE) $(LDFLAGS) -o rk4_c rk4_tl.cpp libbase.a

rk4_f : $(cfiles) dns_ops.h rk4_tl.cpp libbase.a
	$(CXX) -DRK4_FLOOP $(DEFS) $(CXXFLAGS) $(INCLUDE) $(LDFLAGS) -S -o rk4_f.s rk4_tl.cpp libbase.a
	$(CXX) -DRK4_FLOOP $(DEFS) $(CXXFLAGS) $(INCLUDE) $(LDFLAGS) -o rk4_f rk4_tl.cpp libbase.a

rk4_c2 : $(cfiles) dns_ops.h rk4_tl.cpp libbase.a
	$(CXX) -DRK4_2LOOPS $(DEFS) $(CXXFLAGS) $(INCLUDE) $(LDFLAGS) -S -o rk4_c2.s rk4_tl.cpp libbase.a
	$(CXX) -DRK4_2LOOPS $(DEFS) $(CXXFLAGS) $(INCLUDE) $(LDFLAGS) -o rk4_c2 rk4_tl.cpp libbase.a

vorticity_3xyz_c : $(cfiles) dns_ops.h vorticity_tl.cpp libbase.a
	$(CXX) -DVOR_3XYZ_C $(DEFS) $(CXXFLAGS) $(INCLUDE) $(LDFLAGS) -o vorticity_3xyz_c vorticity_tl.cpp libbase.a

vorticity_3xyz_f : $(cfiles) dns_ops.h vorticity_tl.cpp libbase.a
	$(CXX) -DVOR_3XYZ_F $(DEFS) $(CXXFLAGS) $(INCLUDE) $(LDFLAGS) -o vorticity_3xyz_f vorticity_tl.cpp libbase.a

vorticity_xy3z_c : $(cfiles) dns_ops.h vorticity_tl.cpp libbase.a
	$(CXX) -DVOR_XY3Z_C $(DEFS) $(CXXFLAGS) $(INCLUDE) $(LDFLAGS) -o vorticity_xy3z_c vorticity_tl.cpp libbase.a

vorticity_xy3z_f : $(cfiles) dns_ops.h vorticity_tl.cpp libbase.a
	$(CXX) -DVOR_XY3Z_F $(DEFS) $(CXXFLAGS) $(INCLUDE) $(LDFLAGS) -o vorticity_xy3z_f vorticity_tl.cpp libbase.a

fft1d_tl : fft1d_tl.cpp $(cfiles)
	$(CXX) $(DEFS) $(CXXFLAGS) $(INCLUDE) $(LDFLAGS) -o fft1d_tl fft1d_tl.cpp $(cobjs) $(FFTW_LIBS)

fft1d_tl_r : fft1d_tl.cpp $(cfiles)
	$(CXX) $(DEFS) -DFFT_R2C $(CXXFLAGS) $(INCLUDE) $(LDFLAGS) -o fft1d_tl_r fft1d_tl.cpp $(cobjs) $(FFTW_LIBS)

rk4 : dns_ops.h rk4.cpp $(fobjs) $(cobjs) 
	$(CXX) $(DEFS) $(CXXFLAGS) $(INCLUDE) $(LDFLAGS) -o rk4 rk4.cpp $(fobjs) $(cobjs)

vorticity : dns_ops.h vorticity.cpp $(fobjs) $(cobjs) 
	$(CXX) $(DEFS) $(CXXFLAGS) $(INCLUDE) $(LDFLAGS) -o vorticity vorticity.cpp $(fobjs) $(cobjs)

fft1d : fft1d.cpp
	$(CXX) $(DEFS) $(CXXFLAGS) $(INCLUDE) $(LDFLAGS) -o fft1d fft1d.cpp $(FFTW_LIBS)

fft1d_r : fft1d.cpp
	$(CXX) $(DEFS) -DFFT_R2C $(CXXFLAGS) $(INCLUDE) $(LDFLAGS) -o fft1d_r fft1d.cpp $(FFTW_LIBS)                                            

rk4_ibm : rk4_ibm.cpp 
	$(CXX) $(DEFS) $(CXXFLAGS) $(INCLUDE) $(LDFLAGS) -o rk4_ibm rk4_ibm.cpp 

# utility libraries
communicator.o : $(COMMDIR)mpi/communicator.cpp
	$(CXX) $(DEFS) $(CXXFLAGS) $(INCLUDE) -c $(COMMDIR)mpi/communicator.cpp -o communicator.o

.PHONY: clean
clean: 
	touch *.cpp
	rm -rf *.o 
